package org.dromara.system.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import org.dromara.common.core.constant.Constants;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.system.domain.WbItemFile;
import org.dromara.system.domain.dto.NumDTO;
import org.dromara.system.domain.WbMinuteBidBag;
import org.dromara.system.domain.dto.WbCandidateListDTO;
import org.dromara.system.domain.dto.WbItemInfoDTO2;
import org.dromara.system.domain.vo.WbCandidateListVo;
import org.dromara.system.domain.vo.WbMinuteListVo;
import org.springframework.stereotype.Service;
import org.dromara.system.domain.bo.WbItemInfoBo;
import org.dromara.system.domain.vo.WbItemInfoVo;
import org.dromara.system.domain.WbItemInfo;
import org.dromara.system.mapper.WbItemInfoMapper;
import org.dromara.system.service.IWbItemInfoService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 项目Service业务层处理
 *
 * @author Lion Li
 * @date 2024-03-22
 */
@RequiredArgsConstructor
@Service
public class WbItemInfoServiceImpl implements IWbItemInfoService {

    private final WbItemInfoMapper baseMapper;

    /**
     * 更新状态
     */
    @Override
    public void updateStatus() { baseMapper.updateStatus(); }

    /**
     * 查询项目
     */
    @Override
    public WbItemInfoVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询项目列表
     */
    @Override
    public TableDataInfo<WbItemInfoVo> queryPageList(WbItemInfoBo bo, PageQuery pageQuery) {
        Long userId = LoginHelper.getUserId();
        LambdaQueryWrapper<WbItemInfo> lqw = Wrappers.lambdaQuery();
        lqw.in(CollectionUtil.isNotEmpty(bo.getDocTypes()),WbItemInfo::getDocType,bo.getDocTypes());
        lqw.like(StringUtils.isNotBlank(bo.getItemName()), WbItemInfo::getItemName, bo.getItemName());
        lqw.eq(bo.getCategoryId() != null, WbItemInfo::getCategoryId, bo.getCategoryId());
        lqw.eq(bo.getStatus() != null, WbItemInfo::getStatus, bo.getStatus());
        lqw.like(StringUtils.isNotBlank(bo.getItemName()),WbItemInfo::getItemName,bo.getItemName());
        lqw.orderByDesc(WbItemInfo::getPublishTime ,WbItemInfo::getId);
        //过滤草稿状态的数据
        lqw.ne(WbItemInfo::getStatus, Constants.STATUS_DRAFT);
        Page<WbItemInfoVo> result = baseMapper.selectVoPageWithFollow(userId,pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询后端项目列表
     */
    @Override
    public TableDataInfo<WbItemInfoVo> queryHdPageList(WbItemInfoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<WbItemInfo> lqw = buildQueryWrapper(bo);
//        lqw.orderByAsc(WbItemInfo::getStatus);
        lqw.orderByDesc(WbItemInfo::getPublishTime,WbItemInfo::getId);
        lqw.select(WbItemInfo.class, info -> !info.getColumn().equals("prj_introduce") && !info.getColumn().equals("chg_notice_cont"));// 分页查询排除该列
        Page<WbItemInfoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询项目列表
     */
    @Override
    public List<WbItemInfoVo> queryList(WbItemInfoBo bo) {
        LambdaQueryWrapper<WbItemInfo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    /**
     *
     * @param
     * @return
     */
    private LambdaQueryWrapper<WbItemInfo> buildQueryWrapper(WbItemInfoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<WbItemInfo> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getItemName()), WbItemInfo::getItemName, bo.getItemName());
        lqw.eq(bo.getCategoryId() != null, WbItemInfo::getCategoryId, bo.getCategoryId());
        lqw.eq(bo.getItemId() != null, WbItemInfo::getItemId, bo.getItemId());
        lqw.eq(bo.getStatus() != null, WbItemInfo::getStatus, bo.getStatus());
        lqw.eq(bo.getDocType() != null, WbItemInfo::getDocType, bo.getDocType());
        lqw.in(CollectionUtil.isNotEmpty(bo.getDocTypes()),WbItemInfo::getDocType,bo.getDocTypes());
//        lqw.orderByDesc(WbItemInfo::getPublishTime);
        return lqw;
    }

    /**
     * 新增项目
     */
    @Override
    public Boolean insertByBo(WbItemInfoBo bo) {
        WbItemInfo add = MapstructUtils.convert(bo, WbItemInfo.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改项目
     */
    @Override
    public Boolean updateByBo(WbItemInfoBo bo) {
        WbItemInfo update = MapstructUtils.convert(bo, WbItemInfo.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(WbItemInfo entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除项目
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public TableDataInfo<WbItemInfoVo> queryListItemLogs(WbItemInfoBo bo,Long userId, PageQuery pageQuery) {
        return TableDataInfo.build(baseMapper.queryListItemLogs(bo,userId,pageQuery.build()));
    }

    @Override
    public R insertByBoList(List<WbItemInfo> wbItemInfoBos) {
        baseMapper.deleteAll();
        boolean b = baseMapper.insertByBoList(wbItemInfoBos);
        if (b){
            return R.ok();
        }
        return R.fail();
    }

    @Override
    public TableDataInfo<WbItemInfoVo> queryPageListZdy(Long enterpriseId,WbItemInfoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<WbItemInfo> lqw = buildQueryWrapper(bo);
        Page<WbItemInfoVo> result = baseMapper.selectVoPageZdy(enterpriseId,pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    @Override
    public TableDataInfo<WbCandidateListDTO> queryListZdy(Long enterpriseId,PageQuery pageQuery) {
        Page<WbCandidateListDTO> list = baseMapper.selectVoListZdy(enterpriseId,pageQuery.build());
        return TableDataInfo.build(list);
    }

    @Override
    public WbItemInfoVo queryGenerateBYId(Long itemID) {
        return baseMapper.queryGenerateBYId(itemID);
    }

    @Override
    public TableDataInfo<WbItemInfoVo> queryListAllBidsWon(WbItemInfoBo bo, PageQuery pageQuery) {
        return TableDataInfo.build(baseMapper.selectListAllBidsWon(pageQuery.build()));
    }

    @Override
    public TableDataInfo<WbMinuteListVo> queryPageListZYT(Long enterpriseId,String name,  PageQuery pageQuery) {

        return TableDataInfo.build(baseMapper.selectListAllBidsWonZTY(enterpriseId,name,pageQuery.build()));
    }

    @Override
    public TableDataInfo<WbCandidateListDTO> queryListZ(Long enterpriseId, Long userId) {
        List<WbCandidateListDTO> list = baseMapper.selectVoListZ(enterpriseId,userId);
        return TableDataInfo.build(list);
    }

    @Override
    public List<NumDTO> countNum() {
        return baseMapper.selectCountNum();
    }
}
